import React, {Component} from 'react'
import DataCategory from "../../../../types/DataCategory";
import {connect, ConnectedProps} from "react-redux";
import {getRes} from "../../../../configFile";
import {Input, message, Table} from "antd";
import {listDataCategory, queryCategoryAmount, searchCategory} from "../../../../requests/DataCategory";
import {RootState} from "../../../../reducers";

// @ts-ignore
interface PropsFromRedux extends ConnectedProps<typeof connector> { }
interface IState {
    DataCategorySource: Array<DataCategory & { key: number }>
    amount: number
    offset: number
    limit: number
    isSearch: boolean
}
const { Search } = Input;
export class DataCategoryInfo extends Component<PropsFromRedux, IState> {

    constructor(props: PropsFromRedux) {
        super(props)
        this.state = {
            DataCategorySource: [],
            amount: 0,
            offset: 0,
            limit: 10,
            isSearch: false
        }
    }
    //获取数据类型数据
    getDataCategory = async () => {
        try {
            const amountRes = getRes(await queryCategoryAmount())
            if (amountRes.isSuccessful === false) {
                message.error(amountRes.message)
                return;
            }
            const res = getRes(await listDataCategory(this.state.offset, this.state.limit))
            if (res.isSuccessful === false) {
                message.error(res.message)
                return;
            }
            this.setState({
                DataCategorySource: res.data.map((item: DataCategory) => Object.assign({}, item, { key: item.dataCategoryID })),
                amount: amountRes.data
            })
        }
        catch (error) {
            message.error(error.toString())
        }
    }

    async componentDidMount() {
        await this.getDataCategory()
    }

    columns = [{
        title: '数据类型名',
        dataIndex: 'dataCategoryName',
        key: 'dataCategoryName',
        width: 100
    },
    {
        title: '数据类型',
        dataIndex: 'dataType',
        key: 'dataType',
        width: 100,
    }]

    onSearch: (values: string) => void = async values => {
        console.log(values)
        if(values===''){
            await this.getDataCategory()
        }else{
            try {
                const res = getRes(await searchCategory(values))
                if (!res.isSuccessful) {
                    console.error(res);
                    message.error("查询失败")
                }
                this.setState({ DataCategorySource: res.data, amount: res.data.length, isSearch: true })
            } catch (err) {
                message.error(JSON.stringify(err))
            }
        }
    }
    render() {
        return (
            <div>
                <Search placeholder="搜索" onSearch={values => this.onSearch(values)} allowClear={true} enterButton />
                <Table columns={this.columns}
                    dataSource={this.state.DataCategorySource}
                    pagination={{
                        onChange: this.state.isSearch ? undefined : (page, pageSize) => {
                            console.log(page, pageSize);
                            if (!!pageSize) {
                                this.setState({
                                    offset: (page - 1) * (pageSize),
                                    limit: (pageSize)
                                },()=>{
                                    this.getDataCategory()
                                })
                            } else {
                                console.error('pageSize === undefinde')
                            }
                        },
                        defaultPageSize: this.state.limit,
                        total: this.state.amount,
                        showSizeChanger: false
                    }} />
            </div>
        )
    }
}


const mapStateToProps = (state: RootState) => {
    return {
        userID: state.loginReducers.userID
    }
}

const connector = connect(mapStateToProps)

export default connector(DataCategoryInfo)
